package com.cms.admin.action.common;

import com.cms.admin.action.BaseAction;
import com.cms.admin.model.*;
import com.cms.admin.service.common.EntityService;
import com.cms.admin.service.common.TemplateService;
import com.cms.admin.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * Created by YYY-Work on 2017/4/28.
 */
public class EntityListAction extends BaseAction implements ModelDriven<EntityListExt>{

    private TemplateService templateService;
    private EntityService entityService;
    private EntityListExt search=new EntityListExt();
    private PageBean page;
    private EntityList entityList;

    public String requestEntityList()
    {
        return "success";
    }

    public String requestEditEntity(){

        initBathPath();
        List<EntityList> entityLists=entityService.getEntityListByEntityID(search);

        if (null!=entityLists&&entityLists.size()>0)
        {
            setEntityList(entityLists.get(0));
            return "success";
        }else
        {
            return null;
        }
    }

    public String requestAddEntity()
    {
        return "success";
    }

    public String ajaxQueryEntityList()
    {
        LOG.info("Start ArticleAction process,method is ajaxQuery");
        LOG.debug("Action is:" + search.getAction());
        LOG.info("分页查询------currentPage:",String.valueOf(search.getCurrentPage()));
        LOG.info("分页查询------PageSize:",String.valueOf(search.getPageSize()));
        page = new PageBean(search.getCurrentPage(), search.getPageSize());

        List<EntityList> entityList=entityService.queryEntityList(page,search);

        String res=null;

        try {
            res=getQueryResult(entityList,page);

            JSONObject o = new JSONObject();
            o.put("pageCount", page.getTotalPage());
            o.put("content", res);
            return ajaxJsonSuccessMessage(o);
        }catch (Exception e)
        {
            LOG.error("ajaxQueryHospitalList",e);
            return ajaxJsonErrorMessage(Constants.INFO_SYS_ERROR);
        }
    }


    public String ajaxAddEntity()
    {
        request= ServletActionContext.getRequest();
        initBathPath();
        List<EntityList> entityLists=entityService.getEntityListByName(search);
        if (null==entityLists||entityLists.size()==0)
        {
            search.setCreateDate(new Date());
            search.setUpdateDate(new Date());
            search.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
            entityService.addEntityList(search);
        }else
        {
            System.out.print("该医院已经存在");
        }
        return "success";
    }

    public String ajaxEditEntity()
    {
        request=ServletActionContext.getRequest();
        initBathPath();
        search.setUpdateDate(new Date());
        search.setUpdateUserid(((UserList)request.getSession().getAttribute(Constants.USER_OBJECT)).getUserid());
        entityService.updateEntityByEntityID(search);
        return "success";
    }

    private String getQueryResult(List<EntityList> entityList, PageBean page)
            throws Exception {
        initBathPath();
        HashMap map = new HashMap();
        map.put("entityList", entityList);
        map.put("page", page);
        map.put("basePath", getBasePath());

        String result = "";
        result = templateService.generateContentByFreeMarker(
                Constants.TEMPLATE_ENTITY_QUERY_RESULT, map);
        return result;
    }

    @Override
    public EntityListExt getModel() {
        return search;
    }

    public TemplateService getTemplateService() {
        return templateService;
    }

    public void setTemplateService(TemplateService templateService) {
        this.templateService = templateService;
    }

    public EntityService getEntityService() {
        return entityService;
    }

    public void setEntityService(EntityService entityService) {
        this.entityService = entityService;
    }

    public EntityList getEntityList() {
        return entityList;
    }

    public void setEntityList(EntityList entityList) {
        this.entityList = entityList;
    }
}
